#include "log_reg.h"
#include "ui_log_reg.h"
#include "regist_wid.h"
#include "ui_regist_wid.h"
#include <QTcpSocket>
#include <QHostAddress>

#define SERVER_IP "192.168.2.17"
#define SERVER_PORT 9999
log_reg::log_reg(QWidget *parent)
    : QWidget(parent)
       ,ui(new Ui::log_reg)
{
    ui->setupUi(this);
      SetUi();
    tcp = new QTcpSocket(this); //创建QTcpSocket对象
    tcp->connectToHost(QHostAddress(SERVER_IP),SERVER_PORT);  //连接到tcp服务器
    connect(tcp,&QTcpSocket::connected,this,[=](){
        qDebug()<<"ljcg";  //连接成功时候 触发connected信号
    });
    ptr_reg= new regist_wid(tcp);
     connect(ptr_reg,&regist_wid::regwid_closed,this,&log_reg::show);

}
log_reg::~log_reg()
{
    delete ui;
}
void log_reg:: SetUi()
{

     // 设置字体和焦点
     QFont font("Microsoft YaHei");
     this->setFont(font);
     ui->line_name->setFocus();

     // 使用单次定时器确保窗口显示后居中
     QTimer::singleShot(100, this, [this]() {
         QScreen *screen = QGuiApplication::primaryScreen();
         QRect screenGeometry = screen->geometry();
         this->move((screenGeometry.width() - this->width()) / 2,
                   (screenGeometry.height() - this->height()) / 2);
     });
}
void log_reg::on_go_regist_clicked()
{
    qDebug()<<"goto_register";
    ptr_reg->show();
    this->hide();
}

void log_reg::on_login_clicked()
{
    //获取密码用户名并进行tcp通信判断

    QString name= ui->line_name->text();
    qDebug()<<"name:"<<name;
    QString pswd= ui->line_pwd->text();
   ui->line_pwd->setEchoMode(QLineEdit::Password);  // 密码模式（显示为 ••••）
    qDebug()<<"pswd:"<<pswd;


    if (name.isEmpty() || pswd.isEmpty()) {
        QString error;
        if (name.isEmpty()) error += "• 用户名不能为空\n";
        if (pswd.isEmpty()) error += "• 密码不能为空";

        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText("以下问题需要修正：");
        msgBox.setInformativeText(error);
        msgBox.exec();
        return;
    }
    //发送命令、账号和密码
    tcp->write("L ");
    tcp->write(name.toLocal8Bit());
    tcp->write(" ");
    tcp->write(pswd.toLocal8Bit());
    tcp->write(" ");

       TcpDataReceiver recevier(tcp,this);
       recevier.setReadMode(TcpDataReceiver::ReadFixedLength, 2);
       QByteArray data2 = recevier.readData();
       qDebug()<<"respense2:"<<data2;

       if (!data2.isEmpty()) {
           if (data2[0]== 'N'){
                            qDebug()<<"sfskj"<< data2[1];
                             if(data2[1]=='N'){
                                        QMessageBox::information(this, "登录", "登录失败");
                                         return;
                                    }else{
                                        QMessageBox::information(this, "登录", "查询失败");
                                         return;
                             }
                         }
                       else if (data2[0]== 'Y') {
                            // 登录成功，根据用户类型跳转
                               qDebug()<<"yes";
                               main_ptr = new MainWindow(tcp);
                               main_ptr->show();
                                 this->close();
                       }
       }
}
